package cn.snews.Servlet;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.News;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.NewsDAO;

public class UpdateServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9160896234599137830L;

	/**
	 * Constructor of the object.
	 */
	public UpdateServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	String newName(String name) {// 随机生成一个32位
		int pos = name.lastIndexOf('.');
		String prefix = name.substring(pos == -1 ? 0 : pos);
		return UUID.randomUUID() + prefix;
	}

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		response.setContentType("text/html;charset=utf-8");

		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload up = new ServletFileUpload(factory);
		up.setHeaderEncoding("utf-8");// 设置字符集
		List<FileItem> its = null;
		try {
			its = up.parseRequest(request);
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		String picPath = request.getSession().getServletContext()
				.getRealPath("images/");// 设置图片的存放路径
		News news = new News();
		for (FileItem f : its) {
			String fieldName = f.getFieldName();
			if (f.isFormField()) {// 是普通文件
				if (fieldName.equals("ntitle")) {
					news.setNtitle(f.getString("utf-8"));
				} else if (fieldName.equals("nauthor")) {
					news.setNauthor(f.getString("utf-8"));
				} else if (fieldName.equals("nsummary")) {
					news.setNsummary(f.getString("utf-8"));
				} else if (fieldName.equals("ncontent")) {
					news.setNcontent(f.getString("utf-8"));
				} else if (fieldName.equals("nid")) {
					news.setNid(Integer.parseInt(f.getString("utf-8")));
				} else if (fieldName.equals("ntid")) {
					news.setNtid(Integer.parseInt(f.getString("utf-8")));
				}

			} else {// 上传文件
				if (f.getName() == "") {
					continue;
				}
				String p = f.getName().substring(
						f.getName().lastIndexOf('.') + 1);
				List<String> l = Arrays.asList("gif", "bmp", "jpg", "png");
				if (!l.contains(p)) {
					String kk = "修改失败!!!";
					request.setAttribute("kk", kk);
					request.getRequestDispatcher("/WEB-INF/jsp/err.jsp")
							.forward(request, response);
					return;
				} else {
					String newPath = newName(f.getName());
					try {
						f.write(new File(picPath, newPath));
					} catch (Exception e) {
						e.printStackTrace();
					}
					news.setNpicPath("images/" + newPath);
				}
			}
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
		try {
			news.setNcreateDate(sdf.parse(sdf.format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String kk = "修改成功!!!";
		int k = NewsDAO.DAO.update(news);
		System.out.println(k);
		request.setAttribute("k", k);
		request.setAttribute("kk", kk);
		request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(
				request, response);

	}

	public void init() throws ServletException {

	}

}
